package main

import (
	"etcdview/client"
	"etcdview/mylog"
	"etcdview/mysocket"
	"fmt"
	"github.com/gin-gonic/gin"
	"io"
	"os"
	"os/exec"
	"runtime"
)

func main() {
	start()

}

var commands = map[string]string{
	"windows": "cmd /c start",
	"darwin":  "open",
	"linux":   "xdg-open",
}

func Open(uri string) error {
	run, ok := commands[runtime.GOOS]
	if !ok {
		return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS)
	}
	cmd := exec.Command(run, uri)
	return cmd.Start()
}
func start() {
	r := gin.Default()
	logFile, logErr := os.OpenFile("etcdview.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
	if logErr != nil {
		fmt.Println("Fail to find", *logFile, "cServer start Failed")
		os.Exit(1)
	}
	gin.DefaultWriter = io.MultiWriter(logFile, os.Stdout)
	r.LoadHTMLGlob("tem/**/*")
	r.Static("/static", "./static")
	r.GET("/", func(context *gin.Context) {
		context.HTML(200, "index.html", gin.H{
			"title": "EtcdViewSL",
		})
	})
	// 绑定接口
	r.GET("/init", mysocket.Init)
	r.POST("/bind", mysocket.BindIP)
	r.POST("/getCols", mysocket.GetCols)
	r.POST("/getValue", client.GetValue)
	Open("http://127.0.0.1:14462")
	r.Run("127.0.0.1:14462")
}

func init() {
	mylog.LogInit("debug")
}
